package com.sinven.common;

import java.util.List;

/**
 * 分类工具类
 * 
 * @author <a href="mailto:jianfengzhou@139.com">
 * @version 1.0.0
 * @date 2019-05-16
 */
public class Pager<T> {

	// 总记录数
	private long totalRecords;
	// 总页数
	private long totalPages;
	// 当前页
	private long curPage = 1;
	// 每页显示数量
	private int pageSize = 10;
	// 查询起始索引
	private long startIndex;
	// 起始页码
	private long startPage = 1;
	// 结束页码
	private long endPage;
	// 是否第一页
	private boolean firstPage = false;
	// 是否最后一页
	private boolean lastPage = false;
	// 页面数据
	private List<T> datas;

	/**
	 * 分页信息封装
	 * @param totalRecords　总记录数
	 * @param curPage　当前页
	 * @param pageSize　每页显示数量
	 */
	public Pager(long totalRecords, Long curPage, Integer pageSize) {
		this.totalRecords = totalRecords;

		if (pageSize != null && pageSize > 0) {
			this.pageSize = pageSize;
		}
		// 计算总页数
		this.totalPages = this.totalRecords % this.pageSize == 0 ? this.totalRecords / this.pageSize
				: this.totalRecords / this.pageSize + 1;

		// 赋值当前页
		if (curPage != null && curPage > 0) {
			this.curPage = curPage;
		}
		if (this.curPage >= this.totalPages) {
			this.curPage = this.totalPages;
			this.lastPage = true;
		}
		if (this.curPage == 1) {
			this.firstPage = true;
		}
		// 计算查询起始索引
		this.startIndex = (this.curPage - 1) * this.pageSize;
		//计算显示的分页页码范围
		if(this.totalPages <= 10) {
			this.startPage = 1;
			this.endPage = this.totalPages;
		}else {
			//从当前页为第七页开始变化页码范围
			if(this.curPage >= 7) {
				this.startPage = this.curPage - 5;
				this.endPage = this.curPage + 4;
			}
			//页码终值判断
			if(this.endPage >= totalPages) {
				this.endPage = this.totalPages;
				this.startPage = this.endPage - 9;
			}
		}
		
	}

	public long getTotalRecords() {
		return totalRecords;
	}

	public long getTotalPages() {
		return totalPages;
	}

	public long getCurPage() {
		return curPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public long getStartIndex() {
		return startIndex;
	}

	public long getStartPage() {
		return startPage;
	}

	public long getEndPage() {
		return endPage;
	}

	public boolean isFirstPage() {
		return firstPage;
	}

	public boolean isLastPage() {
		return lastPage;
	}

	public List<T> getDatas() {
		return datas;
	}

	public void setDatas(List<T> datas) {
		this.datas = datas;
	}

	@Override
	public String toString() {
		return "Pager [totalRecords=" + totalRecords + ", totalPages=" + totalPages + ", curPage=" + curPage
				+ ", pageSize=" + pageSize + ", startIndex=" + startIndex + ", startPage=" + startPage + ", endPage="
				+ endPage + ", firstPage=" + firstPage + ", lastPage=" + lastPage + ", datas=" + datas + "]";
	}

	
}
